define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'line/line_orders/index' + location.search,
                    add_url: 'line/line_orders/add',
                    edit_url: 'line/line_orders/edit',
                    del_url: 'line/line_orders/del',
                    multi_url: 'line/line_orders/multi',
                    import_url: 'line/line_orders/import',
                    table: 'line_orders',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "nickname").data("orderBy", "id desc");
                $("input[name='spot_id']", form).addClass("selectpage").data("source", "line/line_spot/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                $(form).on("reset", function() {
                    $('.selectpage', form).selectPageClear();
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'spot_id', title: __('Spot_id'),
                            formatter: function (value, row, index) {
                                if (row.spot === null){
                                    test = '无';
                                }else{
                                    test = row.spot.name;
                                }
                                return '<a href="javascript:;" class="searchit btn-spot_id" data-toggle="tooltip" data-original-title="点击搜索 '+test+'"> ' + test + '</a>';
                            },
                            events: {
                                //格式为：方法名+空格+DOM元素
                                'click .btn-spot_id': function (e, value, row, index) {
                                    e.stopPropagation();
                                    var container = $("#table").data("bootstrap.table").$container;
                                    //这里我们手动将数据填充到表单然后提交
                                    $("form.form-commonsearch [name='spot_id']", container).val(value);
                                    $("form.form-commonsearch [name='spot_id']", container).selectPageRefresh();
                                    $("form.form-commonsearch", container).trigger('submit');
                                }
                            },
                        },
                        {
                            field: 'user_id', title: __('User_id'),
                            formatter: function (value, row, index) {
                                if (row.user === null){
                                    test = '无';
                                }else{
                                    test = row.user.nickname;
                                }
                                return '<a href="javascript:;" class="searchit btn-user_id" data-toggle="tooltip" data-original-title="点击搜索 '+test+'"> ' + test + '</a>';
                            },
                            events: {
                                //格式为：方法名+空格+DOM元素
                                'click .btn-user_id': function (e, value, row, index) {
                                    e.stopPropagation();
                                    var container = $("#table").data("bootstrap.table").$container;
                                    //这里我们手动将数据填充到表单然后提交
                                    $("form.form-commonsearch [name='user_id']", container).val(value);
                                    $("form.form-commonsearch [name='user_id']", container).selectPageRefresh();
                                    $("form.form-commonsearch", container).trigger('submit');
                                }
                            },
                        },
                        {
                            field: 'coupon_id', title: __('Coupon_id'),
                            formatter: function (value, row, index) {
                                if (row.coupon === null){
                                    test = '无';
                                }else{
                                    test = row.coupon.coupon.name;
                                }
                                return '<a href="javascript:;" class="searchit btn-coupon_id" data-toggle="tooltip" data-original-title="点击搜索 '+test+'"> ' + test + '</a>';
                            },
                        },
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'pay_amount', title: __('Pay_amount'), operate:'BETWEEN'},
                        {field: 'coupon_amount', title: __('Coupon_amount'), operate:'BETWEEN'},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('Status 7')}, formatter: Table.api.formatter.status},
                        {field: 'child_count', title: __('Child_count')},
                        {field: 'adult_count', title: __('Adult_count')},
                        {field: 'day', title: __('Day'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'refund_order_sn', title: __('Refund_order_sn'), operate: 'LIKE'},
                        {field: 'reason', title: __('Reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bill', title: __('Bill'),  operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'canceltime', title: __('Canceltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', width:100, title: __('Operate'), buttons: [
                                {
                                    name: 'members',
                                    text: __('出行人'),
                                    title: __('出行人'),
                                    dropdown:'更多',
                                    classname: 'btn btn-success btn-dialog',
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    icon: 'fa fa-list',
                                    url: 'line/line_orders_members/index?order_id={id}',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'refund',
                                    text: __('退押金'),
                                    title: __('退押金'),
                                    dropdown:'更多',
                                    classname: 'btn btn-success btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'line/line_orders/refund?order_id={id}',
                                    confirm: '确认退押金？',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status === 6){
                                            return true;
                                        }else{
                                            return false;
                                        }

                                    }
                                },
                            ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'line/line_orders/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'line/line_orders/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'line/line_orders/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
